package com.tensquare.friend.service;

import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.pojo.Friend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class FriendService {
    @Autowired
    private FriendDao friendDao;
    //1.添加关注
    public boolean add(String id, String friendid) {
        //1.1判断之前是否添加过,添加过直接返回false
        if (friendDao.findByIdAndFriendid(id,friendid) != null) return false;
        //1.2查询要关注的对象是否关注了自己 , 关注的话两人的islike都为 1 ( 添加的时候直接设置islike属性 )
        if (friendDao.findByIdAndFriendid(friendid,id) != null){
            friendDao.save(new Friend(id,friendid,"1"));
            friendDao.updateIsLike(friendid,id,"1");
        }else {
            friendDao.save(new Friend(id,friendid,"0"));
        }
        return true;
    }
    //2.取消关注
    public boolean delete(String id,String friendid) {
        //2.1查不到friend直接返回false , 说明未关注 ,否则直接删除
        if (friendDao.findByIdAndFriendid(id,friendid) == null) return false;
        friendDao.deleteByIdAndFriendid(id,friendid);
        //2.2 如果对方也关注了此用户 , 则修改islike为 0
        friendDao.updateIsLike(friendid,id,"0");
        return true;
    }
}
